/**
 * Created by sam on 16-12-14.
 */
var domain = {
    'us':'https://www.amazon.com',
    'ca':'https://www.amazon.ca',
    'jp':'https://www.amazon.co.jp',
    'uk':'https://www.amazon.co.uk',
    'de':'https://www.amazon.de',
    'es':'https://www.amazon.es',
    'fr':'https://www.amazon.fr',
    'it':'https://www.amazon.it',
};

var domainDp = {
    'us':'https://www.amazon.com/dp/',
    'ca':'https://www.amazon.ca/dp/',
    'jp':'https://www.amazon.co.jp/dp/',
    'uk':'https://www.amazon.co.uk/dp/',
    'de':'https://www.amazon.de/dp/',
    'es':'https://www.amazon.es/dp/',
    'fr':'https://www.amazon.fr/dp/',
    'it':'https://www.amazon.it/dp/',
};
//
var domainPr = {
    'us':'https://www.amazon.com/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'ca':'https://www.amazon.ca/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'jp':'https://www.amazon.co.jp/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'uk':'https://www.amazon.co.uk/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'de':'https://www.amazon.de/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'es':'https://www.amazon.es/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'fr':'https://www.amazon.fr/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
    'it':'https://www.amazon.it/*/product-reviews/{ASIN}/ref=cm_cr_arp_d_viewopt_srt?ie=UTF8&reviewerType=avp_only_reviews&showViewpoints=1&sortBy=recent&formatType=current_format&pageNumber={PAGE}',
};
var domainSear = {
    'us':"https://completion.amazon.com/search/complete?method=completion&mkt=1&c=&p=Gateway&l=en_US&sv=desktop&client=amazon-search-ui&x=String&search-alias=aps&q={KEY}&cf=1&fb=1&sc=1",
    'jp':"https://completion.amazon.co.jp/search/complete?method=completion&mkt=6&c=&p=Gateway&l=ja_JP&sv=desktop&client=amazon-search-ui&x=String&search-alias=aps&q={KEY}&cf=1&fb=1&sc=1",
};

var month = {
  'us':['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  'uk':['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  'ca':['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  'it':['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
  'fr':['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
  'es':['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'],
};

var month_dot = {
    'us':['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'],
    'uk':['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'],
    'ca':['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'],
};

function getDomain() {
    return domain[source];
}

function getDpUrl() {
    return domainDp[source];
}

function getReviewUrl(asin,page) {
    return domainPr[source].replace("{ASIN}",asin).replace("{PAGE}",page);
}

function getSearchKeyUrl(key) {
    return domainSear[source].replace("{KEY}",key);
}

function getMonth(mm) {
    return month[source].indexOf(mm)+1;
}

function getMonthDot(mm) {
    return month_dot[source].indexOf(mm)+1;
}

function getSource() {
    return source;
}

function setSource(sc) {
    source = sc;
}
function getFetchTime() {
    return fetchTime;
}

function setFetchTime(time) {
    fetchTime = time;
}

function getTime(date) {
	if(!date) date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() +1;
    var d = date.getDate();
    return formatTime(y,m,d);
}

function formatTime(y,m,d) {
    return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d);
}

function formatDate(date) {
    var res = "1900-01-01";
    switch(source){
        case 'uk':
            var d = date.replace("on ","").trim().split(" ");
            try{
                var year = d[2];
                var day = parseInt(d[0]);
                var month = '1';
                if(d[1].indexOf(".")>0){
                    month = getMonthDot(d[1]);
                }else{
                    month  =  getMonth(d[1]);
                }
                res = formatTime(year,month,day);
            }catch (e){
                console.log("d--->"+d)
            }
            break;
        case 'us':
            //on September 29, 2016
            var d = date.replace("on ","").trim().split(" ");
            var year = d[2];
            var day = parseInt(d[1]);
            var month  = getMonth(d[0]);
            res = formatTime(year,month,day);
            break;
        case 'ca':
            //on October 3, 2016
            //on Sept. 24 2016
            var d = date.replace("on ","").trim().split(" ");
            try{
                var year = d[2];
                var day = parseInt(d[1]);
                var month = '1';
                if(d[0].indexOf(".")>0){
                    month = getMonthDot(d[0]);
                }else{
                    month  =  getMonth(d[0]);
                }
                res = formatTime(year,month,day);
            }catch (e){
                console.log("d--->"+d)
            }
            break;
        case 'it':
            //il 4 novembre 2016
            var d = date.replace("il ","").trim().split(" ");
            var year = d[2];
            var day = parseInt(d[0]);
            var month  = getMonth(d[1]);
            res = formatTime(year,month,day);
            break;
        case 'fr':
            // le 17 septembre 2016
            var d = date.replace("le ","").trim().split(" ");
            var year = d[2];
            var day = parseInt(d[0]);
            var month  = getMonth(d[1]);
            res = formatTime(year,month,day);
            break;
        case 'es':
            //en 29 de agosto de 2016
            var d = date.replace("en ","").trim().split(" ");
            var year = d[4];
            var day = parseInt(d[0]);
            var month  = getMonth(d[2]);
            res = formatTime(year,month,day);
            break;
    }

    return res;
}

function nextWeek(today){
    if(!today) today = new Date();
    var nextweek = new Date(today.getFullYear(), today.getMonth(), today.getDate()+7);
    return nextweek;
}


function getFirstDateOfWeek(theDate){
    theDate.setDate(theDate.getDate() - theDate.getDay() +1); //
    return theDate;
}



var source = 'uk';
var fetchTime = '2000-01-01';

module.exports.setSource = setSource;
module.exports.getSource = getSource;
module.exports.getFetchTime = getFetchTime;
module.exports.getTime = getTime;

module.exports.setFetchTime = setFetchTime;
module.exports.getDomain = getDomain;
module.exports.formatDate = formatDate;
module.exports.formatTime = formatTime;
module.exports.getDpUrl = getDpUrl;
module.exports.getReviewUrl = getReviewUrl;
module.exports.getMonth = getMonth;
module.exports.getSearchKeyUrl = getSearchKeyUrl;

module.exports.nextWeek = nextWeek;
module.exports.getFirstDateOfWeek = getFirstDateOfWeek;
